package com.atwork.wuhua.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.adapter.BannerAdapter;
import com.atwork.wuhua.adapter.FieldDetialLeftAdapter;
import com.atwork.wuhua.adapter.FieldDetialParentAdapter;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.bean.DataBean;
import com.atwork.wuhua.bean.FieldDetailListBean;
import com.atwork.wuhua.bean.FieldDetialBean;
import com.atwork.wuhua.mvp.presenter.FieldDetiaPresenter;
import com.atwork.wuhua.mvp.view.IFieldDetialActivity;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.StringUtil;
import com.atwork.wuhua.utils.TimeUtil;
import com.atwork.wuhua.utils.UserUtil;
import com.atwork.wuhua.utils.Util;
import com.atwork.wuhua.wrideg.StarBar;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 场地详情
 */

public class FieldDetialActivity extends BaseActivity implements IFieldDetialActivity {


    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.sb_lever)
    StarBar sbLever;
    @BindView(R.id.tv_distance)
    TextView tvDistance;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_tag1)
    TextView tvTag1;
    @BindView(R.id.tv_tag2)
    TextView tvTag2;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_introduce)
    TextView tvIntroduce;
    @BindView(R.id.tv_trafficInfo)
    TextView tvTrafficInfo;
    @BindView(R.id.rv_field_sub)
    RecyclerView rvFieldSub;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    @BindView(R.id.tv_action)
    TextView tvAction;
    @BindView(R.id.rv_field_sub_left)
    RecyclerView rvFieldSubLeft;

    private FieldDetialLeftAdapter fieldDetialLeftAdapter;
    private FieldDetialParentAdapter fieldDetialParentAdapter;
    private String id, site_id, field_id, bespeak_time = "";

    private FieldDetiaPresenter presenter;
    private List<String> bannerList;
    private List<FieldDetailListBean.DataBeanX.TimeSlotBean> fieldDetialLBeans;
    private String lat, log, tel;
    private int total = 0;
    private StringBuffer bespeak_time_sb = new StringBuffer();

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_field_detial;
    }

    @Override
    public void initView() {
        if (getIntent().getExtras() != null) {
            id = getIntent().getExtras().getString(ConstantsMyself.INTENTID);
            LogUtils.e("id==>" + id);
        }
        presenter = new FieldDetiaPresenter();
        presenter.attachView(this);
        presenter.getDetail(id);
        bannerList = new ArrayList<>();
        fieldDetialLBeans = new ArrayList<>();

    }

    @Override
    public void getFieldDetail(FieldDetialBean data) {
        bannerList.clear();
        bannerList.addAll(data.getData().getAlbum());
        initBanner(bannerList);
        tvName.setText(data.getData().getName());
        String discance = Util.getDistance(this, Double.parseDouble(data.getData().getLongitude()), Double.parseDouble(data.getData().getLatitude()));
        tvDistance.setText(discance);
        sbLever.setIsOnTouch(false);
        sbLever.setStarMark(Float.parseFloat(data.getData().getStars()));
        tvTime.setText("开放时间： " + data.getData().getOpen_time());
        tvAddress.setText(data.getData().getAddress());
        tvIntroduce.setText(data.getData().getDescription());
        tvTrafficInfo.setText(data.getData().getInfos());
        if (data.getData().getLabels().size() > 0) {
            tvTag1.setText(data.getData().getLabels().get(0));
        }
        if (data.getData().getLabels().size() > 1) {
            tvTag2.setText(data.getData().getLabels().get(1));
        }
        lat = data.getData().getLatitude();
        log = data.getData().getLongitude();
        tel = data.getData().getTel();
    }

    @Override
    public void getFieldDetailList(FieldDetailListBean data) {
        fieldDetialLBeans.clear();
        initRvInitLeft();
        fieldDetialLBeans.addAll(data.getData().get(0).getTime_slot());
        initRvInitRight(fieldDetialLBeans);
        field_id = data.getData().get(0).getId();
        site_id = data.getData().get(0).getSite_id();
    }

    @Override
    public void bespeakResult() {
        total = 0;
        tvTotal.setText("0场");
        bespeak_time_sb.delete(0, bespeak_time_sb.toString().length());
        bespeak_time = "";
        presenter.getDetail(id);
        presenter.getDetailList(id);
    }

    @Override
    public void initData() {

    }

    //RecyclerView 左边时间段
    private void initRvInitLeft() {
        List<String> leftList = new ArrayList<>();
        leftList.clear();
        leftList.add("场地预约");
        leftList.add("08:00—10:00");
        leftList.add("10:00—12:00");
        leftList.add("12:00—14:00");
        leftList.add("14:00—16:00");
        leftList.add("16:00—18:00");
        leftList.add("18:00—20:00");
        leftList.add("20:00—22:00");
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvFieldSubLeft.setLayoutManager(manager);
        fieldDetialLeftAdapter = new FieldDetialLeftAdapter(R.layout.item_field_detial_left, leftList);
        rvFieldSubLeft.setAdapter(fieldDetialLeftAdapter);

        rvFieldSubLeft.setHasFixedSize(true);
        rvFieldSubLeft.setNestedScrollingEnabled(false);
    }


    //RecyclerView 右边预约选项
    private void initRvInitRight(List<FieldDetailListBean.DataBeanX.TimeSlotBean> data) {


        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvFieldSub.setLayoutManager(manager);
        fieldDetialParentAdapter = new FieldDetialParentAdapter(this, R.layout.item_field_detial_parent, data);
        rvFieldSub.setAdapter(fieldDetialParentAdapter);


        rvFieldSub.setHasFixedSize(true);
        rvFieldSub.setNestedScrollingEnabled(false);

        fieldDetialParentAdapter.setOnChildClickItem(new FieldDetialParentAdapter.OnChildItemClick() {
            @Override
            public void OnChildClickItem(int personPosition, int childPosition) {
                if (!fieldDetialLBeans.get(personPosition).getData().get(childPosition).isIs_reserved()) {
                    if (fieldDetialLBeans.get(personPosition).getData().get(childPosition).isCan_select()) {
                        if (fieldDetialLBeans.get(personPosition).getData().get(childPosition).isCheck()) {
                            fieldDetialLBeans.get(personPosition).getData().get(childPosition).setCheck(false);
                        } else {
                            fieldDetialLBeans.get(personPosition).getData().get(childPosition).setCheck(true);
                        }
                        fieldDetialParentAdapter.setNewData(fieldDetialLBeans);
                        selectTotal(fieldDetialLBeans);
                    }

                }

            }


        });

    }

    private void selectTotal(List<FieldDetailListBean.DataBeanX.TimeSlotBean> fieldDetialLBeans) {

        total = 0;
        bespeak_time_sb.delete(0, bespeak_time_sb.toString().length());
        for (int i = 0; i < fieldDetialLBeans.size(); i++) {
            for (int j = 0; j < fieldDetialLBeans.get(i).getData().size(); j++) {
                if (fieldDetialLBeans.get(i).getData().get(j).isCheck()) {
                    total = total + 1;
                    if (total == 1) {
                        bespeak_time_sb.append(fieldDetialLBeans.get(i).getData().get(j).getBespeak());
                    } else {
                        bespeak_time_sb.append("," + fieldDetialLBeans.get(i).getData().get(j).getBespeak());
                    }
                }
            }
        }
        tvTotal.setText(total + "场");
        bespeak_time = bespeak_time_sb.toString();
        LogUtils.e("total==>" + total);
        LogUtils.e("bespeak_time_sb==>" + bespeak_time_sb.toString());
    }

    private void initBanner(List<String> bannerList) {

        //设置内置样式，共有六种可以点入方法内逐一体验使用。
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器，图片加载器在下方
        banner.setImageLoader(new BannerAdapter());
        //设置图片网址或地址的集合
        banner.setImages(bannerList);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        //设置轮播图的标题集合
//        banner.setBannerTitles(list_title);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);

        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.CENTER)
                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                //必须最后调用的方法，启动轮播图。
                .start();
    }


    @OnClick({R.id.img_left, R.id.tv_address, R.id.img_phone, R.id.tv_action})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.img_left:
                finish();
                break;
            case R.id.tv_address:
                intent = new Intent(this, MapActivity.class);
                intent.putExtra(ConstantsMyself.INTENTLAT, lat);
                intent.putExtra(ConstantsMyself.INTENTLOG, log);
                startActivity(intent);
                break;
            case R.id.img_phone:
                intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + tel);
                intent.setData(data);
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                startActivity(intent);
                break;
            case R.id.tv_action:
                if (UserUtil.isLoginOrOpen(this)) {
                    presenter.bespeak(site_id, field_id, bespeak_time);
                }
                break;
        }
    }


}
